unit FormMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OpenGL_SG, GLFont, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Image1: TImage;
    LabError: TLabel;
    Memo1: TMemo;
    Button1: TButton;
    LabSample: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Button1Click(Sender: TObject);
  private
    DC: HDC;
    RC: HGLRC;
    OpenGLFont: TOpenGLFont;
    function CreateGL: Boolean;
    procedure MakeCurrent;
    { Private-Deklarationen }
  public
    procedure Paint; override;
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.MakeCurrent;
begin
  if (wglGetCurrentContext() <> RC) or
     (wglGetCurrentDC() <> DC) then
    wglMakeCurrent(DC, RC);
end;

function TForm1.CreateGL: Boolean;
var
  PFD: TPixelFormatDescriptor;
  PixelFormat: TGLint;
begin
  Result := InitOpenGL;
  // Set Pixelformat
  FillChar(pfd, SizeOf(TPixelFormatDescriptor), 0);
  pfd.cColorBits := 32;
  pfd.cDepthBits := 32;
  pfd.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
  pfd.nSize := SizeOf(TPixelFormatDescriptor);
  pfd.nVersion := 1;
  pfd.iPixelType := PFD_TYPE_RGBA;
  pfd.iLayerType := PFD_MAIN_PLANE;
  DC := Windows.GetDC(Self.Handle);
  SetLastError(0);
  PixelFormat := ChoosePixelFormat(DC, @pfd);
  if PixelFormat = 0 then
    exit;
  if SetPixelFormat(DC, PixelFormat, @pfd) then
  begin
    RC := wglCreateContext(DC);
    MakeCurrent;
    glClearDepth(1.0);

    glDisable(GL_DITHER);
    glShadeModel(GL_FLAT);
    glDisable(GL_CULL_FACE);
    glDisable(GL_LIGHTING);

    glAlphaFunc(GL_GREATER,0.0);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
{    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_POINT_SMOOTH);
    glEnable(GL_POLYGON_SMOOTH);
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);}
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glMatrixMode(GL_PROJECTION);
    //gluPerspective(90.0, 1, 0.01, 10.0);
    glOrtho(0, ClientWidth, ClientHeight, 0, 0.01, 10);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    // Setup Anti-Aliasing:
    glEnable(GL_TEXTURE_2D);
    glClearColor(0.4, 0.4, 0.4, 1.0);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    Result := true;
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenGL_SG.InitOpenGL;
  if not CreateGL then
    MessageDlg('OpenGL Init failed', mtError, [mbOk], 0);
  OpenGLFont := TOpenGLFont.Create;
  OpenGLFont.FirstGlyph := #32;
  OpenGLFont.LastGlyph := #255;
  OpenGLFont.Font := LabSample.Font;
  //Image1.Picture.Graphic := OpenGLFont.FGlyphMap;
end;

procedure TForm1.Paint;
begin
  LabError.Caption := 'GL Error: ' + gluErrorString(glGetError);
  glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT);
  glLoadIdentity;
  glScalef(2, 2, 1);
  OpenGLFont.DrawText(10, 210, LabSample.Caption, ColorToRGB(OpenGLFont.Font.Color));
  glFlush();
  SwapBuffers(DC);
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_ESCAPE then
    Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenGLFont.Font := Form1.Font;
end;

end.
